<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
</head>

<body>
    <div id="map">
    </div>
    <script>
        var map = new ol.Map({
            target: "map",
            layers: [gaode],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 8
            })
        })
        /* 1、加载geojson数据 */
        var url = 'https://geo.datav.aliyun.com/areas_v3/bound/420100_full.json'
        // 自动将geojson数据格式为new ol.Feature()，并添加到这个数据源中
        var source = new ol.source.Vector({
            format: new ol.format.GeoJSON(),
            url
        })
        var layer = new ol.layer.Vector({
            source
        })
        map.addLayer(layer)
        /* 2、对图层进行事件监听 */
        /* 鼠标移动到要素--抓手 */
        map.on('pointermove', function (e) {
            var pixel = map.getEventPixel(e.originalEvent);
            var hit = map.hasFeatureAtPixel(pixel);
            map.getTargetElement().style.cursor = hit ? 'pointer' : '';
        });
        /* 点击事件-获取元素 */
        var high_style = new ol.style.Style({
            fill: new ol.style.Fill({
                color: "#333"
            })
        })
        map.on('click', function (evt) {
            //判断当前单击处是否有要素，捕获到要素时弹出popup
            // 拿到点击处的要素
            var feature = map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) { return feature; });
            /* 让其他元素不高亮 */
            var features = source.getFeatures();
            features.forEach(item => {
                item.setStyle(null);
            })
            if (feature) {
                /* 3、正在点击的要素--高亮 */
                feature.setStyle(high_style)
            }
        });

        /* 4、其他元素--不要高亮 */
    </script>
</body>

</html>